package com.UnionPayCenter.distribution.manager.impl;

import com.UnionPayCenter.distribution.constants.DistributionConstants;
import com.UnionPayCenter.distribution.constants.TemplateDefaultConstants;
import com.UnionPayCenter.distribution.dto.DistributionBaseDTO;
import com.UnionPayCenter.distribution.entity.DistributionTemplateDO;
import com.UnionPayCenter.distribution.manager.DistributionTemplateManager;
import com.UnionPayCenter.mapper.DistributionTemplateDOMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * @author Noah
 * @since 1.0.0, 2019/07/02
 */
@Service
public class DistributionTemplateManagerImpl implements DistributionTemplateManager {

    @Autowired
    private DistributionTemplateDOMapper distributionTemplateDOMapper;

    /**
     * 模板初始化
     *
     * @param baseDTO 基本信息
     */
    @Override
    public DistributionTemplateDO initTemplate(DistributionBaseDTO baseDTO) {

        DistributionTemplateDO exists = queryTemplate(baseDTO);
        if (Objects.nonNull(exists)) {
            return exists;
        }
        Date date = new Date();
        DistributionTemplateDO templateDO = new DistributionTemplateDO();
        templateDO.setShopId(baseDTO.getShopId());
        templateDO.setBId(baseDTO.getBId());
        templateDO.setDistrId(TemplateDefaultConstants.DISTR_ID);
        templateDO.setShareProp(TemplateDefaultConstants.SHARE_PROP);
        templateDO.setShareType(TemplateDefaultConstants.SHARE_TYPE);
        templateDO.setPreShareFlag(TemplateDefaultConstants.PRE_SHARE_FLAG);
        templateDO.setShareVal(TemplateDefaultConstants.SHARE_VAL);
        templateDO.setActVal(TemplateDefaultConstants.ACT_VAL);
        templateDO.setShareLimit(TemplateDefaultConstants.SHARE_LIMIT);
        templateDO.setActTimeStart(TemplateDefaultConstants.ACT_TIME_START);
        templateDO.setActTimeEnd(TemplateDefaultConstants.ACT_TIME_END);
        templateDO.setShareAmoLimit(TemplateDefaultConstants.SHARE_AMO_LIMIT);
        templateDO.setShareLvl(TemplateDefaultConstants.SHARE_LVL);
        templateDO.setAccrualStart(TemplateDefaultConstants.ACCRUAL_START);
        templateDO.setAccrualEnd(TemplateDefaultConstants.ACCRUAL_END);
        templateDO.setAccType(TemplateDefaultConstants.ACC_TYPE);
        templateDO.setActLev(TemplateDefaultConstants.ACT_LEV);
        templateDO.setActNum(TemplateDefaultConstants.ACT_NUM);
        templateDO.setDisType(TemplateDefaultConstants.DIS_TYPE);
        templateDO.setFlag(TemplateDefaultConstants.flag);
        templateDO.setCreateTime(date);
        templateDO.setUpdateTime(date);
        templateDO.setRemark(TemplateDefaultConstants.remark);
        templateDO.setEffectNextDayFlag(DistributionConstants.FALSE_NUMBER);
        distributionTemplateDOMapper.insertSelective(templateDO);
        return templateDO;
    }

    /**
     * 根据商户，店铺查询分销模板信息
     *
     * @param baseDTO 基本信息
     */
    @Override
    public DistributionTemplateDO queryTemplate(DistributionBaseDTO baseDTO) {
        return distributionTemplateDOMapper.selectByShopId(baseDTO.getBId(), baseDTO.getShopId());
    }

    /**
     * 更新分销模板状态
     *
     * @param baseDTO
     * @param flag
     * @return
     */
    @Override
    public void updateTemplateFlag(DistributionBaseDTO baseDTO, String flag) {
        DistributionTemplateDO exists = queryTemplate(baseDTO);
        if (Objects.isNull(exists)) {
            return;
        }
        DistributionTemplateDO templateDO = new DistributionTemplateDO();
        templateDO.setFlag(flag);
        templateDO.setIdgen(exists.getIdgen());
        distributionTemplateDOMapper.updateByPrimaryKeySelective(templateDO);
    }

    /**
     * 更新模板信息
     *
     * @param templateDO
     * @param baseDTO
     */
    @Override
    public DistributionTemplateDO updateDistributionTemplate(DistributionTemplateDO templateDO, DistributionBaseDTO baseDTO) {
        DistributionTemplateDO exists = queryTemplate(baseDTO);
        if (Objects.isNull(exists)) {
            return null;
        }
        templateDO.setIdgen(exists.getIdgen());
        templateDO.setUpdateTime(new Date());
        distributionTemplateDOMapper.updateByPrimaryKeySelective(templateDO);
        return distributionTemplateDOMapper.selectByPrimaryKey(exists.getIdgen());
    }
}
